<?php

class Welcome extends CI_Controller {

	function __construct(){
		parent::__construct();
		//$this->load->library('FAL_front', 'fal_front'); // Attach one of freakauth libraries
		$this->load->library('form_validation');
		$this->load->library('session');
		$this->load->model('user_man_model');
		$this->load->model('konfigurasi_model');
	}//end constructor
	
	function index(){
		if( $this->session->userdata('userid') == null) {
			$this->login();
		}//end if
		else
		{
			$this->home();			
		}  
	}//end index
	
	function login(){
		$tema = $this->session->userdata('warna_tema');
		$this->session->sess_destroy(); 		
		$valid = false; 
		
		$this->form_validation->set_rules('user_name', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');
		$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
						
		if($this->form_validation->run() == TRUE)
		{
			$username = $this->input->post('user_name');
			$password = $this->input->post('password');
		
			$users	= $this->user_man_model->get_user(); 
			$users_obj	= $users->result(); 
			
			foreach ($users_obj as $row){	
				if($username == $row->USERNAME && md5($password) == $row->PASSWORD){
					$valid = true;
					$newdata = array(
						'userid' => $row->ID_USER,
						'username' => $username ,
						'roleid' => $row->IDROLE,
						'unitid' => $row->ID_SEKOLAH,
						'tahunajaran' => $row->ID_TA,
						'semester' => $row->ID_SMT,
						'warna_tema' => $tema
						);
					$this->session->set_userdata($newdata);
					break;
				}			
			}
			
			if($valid){ 
				$this->home();
			}
			else{
				$data['cek_form'] = 1;
				$nama_ins = $this->konfigurasi_model->get_logo()->row();
				$ket_logo = $this->konfigurasi_model->get_header()->row();
				$data['nama_instansi'] = $nama_ins->NAMA_SEKOLAH;
				$data['url_logo'] = $nama_ins->FOTO;
				$data['text_keterangan'] = $ket_logo->TEXT_KETERANGAN;
				$this->load->view('welcome',$data);
			}
		
		}
		else 
		{
			$this->session->set_userdata('warna_tema', $tema);
			$nama_ins = $this->konfigurasi_model->get_logo()->row();
			$ket_logo = $this->konfigurasi_model->get_header()->row();
			$data['nama_instansi'] = $nama_ins->NAMA_SEKOLAH;
			$data['url_logo'] = $nama_ins->FOTO;
			$data['text_keterangan'] = $ket_logo->TEXT_KETERANGAN;
			$this->load->view('welcome',$data);
		} // end foreach
			 
	}//end login
	
	function home()
	{
		$txt_welcome = $this->konfigurasi_model->get_header()->row()->TEXT_WELCOME;
		if($this->session->userdata('userid') == null) redirect('welcome');
		$data2['text_welcome'] = $txt_welcome;
		$data['content'] = $this->load->view('home',$data2,true);
		$this->load->view('main',$data);
                
	}//end home
        
	function logout()
	{		
		$this->session->sess_destroy();
		$this->login();	
	}
	
	function gantiTema($warna){
		$this->session->set_userdata('warna_tema', $warna);
		$nama_ins = $this->konfigurasi_model->get_logo()->row();
		$ket_logo = $this->konfigurasi_model->get_header()->row();
		$data['nama_instansi'] = $nama_ins->NAMA_SEKOLAH;
		$data['url_logo'] = $nama_ins->FOTO;
		$data['text_keterangan'] = $ket_logo->TEXT_KETERANGAN;
		$this->load->view('welcome', $data);
	}
	
	
}//end class

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
